﻿/// <reference path="jquery/jquery-1.9.1.js" />
$(function () {
    //排序布局
    $.getScript('/Handlers/LoadScripts.ashx?dir=scripts.ui&load=core,widget,mouse,sortable&ver=1.0', function () {
        $('.sortable-box').sortable({
            sort: function (event, ui) { $('.sortable-placeholder').css('height', $('.ui-sortable-helper').height()); },
            placeholder: 'sortable-placeholder',
            connectWith: '.sortable-box',
            cancel: 'div.cancal-sort',
            opacity: 0.8,
            update: function () {
            }
        }).enableSelection();
    });

    //设置slug
    $('#post_title').blur(function () {
        if ($(this).val()) {
            if (!$('#post_slug').val()) {
                $('#post_slug').val($(this).val().replace(/\s+/g, '-'));
            }
        }
    });

    //状态改变
    $('#post_status').change(function () {
        if ($(this).val() == '1') {
            $('.post-password').show();
            $('#post_password').attr('disabled', false);
        } else {
            $('.post-password').hide();
            $('#post_password').empty().attr('disabled', true);
        }
    });

    //获取地址
    $('#tag_marker').click(function () {
        $.getJSON('/handlers/postajax.ashx', { action: 'get_marker' }, function (data) {
            if (data.status == 0) {
                var adds = data.address.split('|');
                $('#post_tag').val(adds[1] + ',' + adds[2]);
            } else {
                $('#post_tag').val('你是火星登陆地球？');
            }
        });
    });

    //DatePicker
    $('#pubdate').datetimepicker({
        language: 'en',
        pick12HourFormat: true
    });
    var picker = $('#pubdate').data('datetimepicker');
    //var date = new Date();
    //picker.setLocalDate(new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes()));

    //Tab选项卡
    $('#category_tabs li').click(function () {
        if ($(this).attr('class') == 'current') return false;
        var $tabs = $('#category_tabs li');
        var $items = $(".tab-inner");
        var clickElement = this;
        $tabs.each(function (i, element) {
            if (clickElement == element) {
                $($tabs[i]).addClass("current");
                $($items[i]).addClass("current");
            }
            else {
                $($tabs[i]).removeClass("current");
                $($items[i]).removeClass("current");
            }
        });
        return false;
    });

    //Publish
    $('#publish_public').click(function () {
        publish($('#post_status').val(), '文章已发布。', '文章发布失败。');
    });

    //Publish
    $('#publish_draft').click(function () {
        publish(-1, '草稿已保存。', '草稿保存失败。');
    });
});

//发布函数
function publish(postStatus, successMsg, errorMsg) {
    clearTimeout(timeOut);
    $('#message').removeAttr('class').hide();
    var postId = $('#post_id').val();
    var postSlug = $('#post_slug').val();
    var postTitle = $('#post_title').val();
    if (!postTitle) {
        showMsg('请填写文章标题。', 'alert-info');
        return false;
    }
    var picker = $('#pubdate').data('datetimepicker');
    var postPubdate = picker.getLocalDate();//$('#post_pubdate').val();
    var postModified = postPubdate;
    var postContent = window.UE.getEditor('post_content').getContent();
    var postExcerpt = $('#post_excerpt').val();
    var postAuthor = $('#post_author').val();
    var postCommentStatus = $('#post_comment_status').val();
    var postStatus = postStatus;
    var postType = $('#post_type').val();
    var postIstop = $('#post_istop').is(':checked');
    var postPingStatus = $('#post_ping_status').val();
    var postPassword = $('#post_password').val();
    var postTag = $('#post_tag').val();
    var postCategorys = new Array();
    $('.current .post-category:checked').each(function (i, item) {
        postCategorys[i] = $(item).val();
    });
    var postCategory = postCategorys.length > 0 ? postCategorys.toString() : '1';
    $('.post-tip').fadeIn();
    $('.btn[data-loading-text]').button('loading');
    var action = postId == 0 ? 'publish' : 'edit';
    $.post('/handlers/postajax.ashx?action=' + action, {
        post_id: postId,
        post_slug: postSlug,
        post_title: postTitle,
        post_pubdate: postPubdate,
        post_modified: postModified,
        post_content: postContent,
        post_excerpt: postExcerpt,
        post_author: postAuthor,
        post_comment_status: postCommentStatus,
        post_status: postStatus,
        post_type: postType,
        post_istop: postIstop,
        post_ping_status: postPingStatus,
        post_password: postPassword,
        post_tag: postTag,
        post_category: postCategory
    }, function (json) {
        $('.button').removeAttr('disabled');
        $('.spinner').fadeOut();
        if (json == 'login_timeout') {
            goToLogin();
            return false;
        }
        var data = $.parseJSON(json);
        if (data.Status == '0') {
            $('#post_id').val(data.PostId);
            showMsg(successMsg + '<a href="' + data.PostLink + '">查看文章</a>', 'alert-success');
        } else {
            showMsg(errorMsg + data.Message, 'alert-error');
        }
    });
    return false;
}